using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Data;

namespace Utils
{
    public static class DB
    {
        public static string DBName;
        public static string DBPass = "a99521.#";
        private static string connStr;
        private static SQLiteConnection conn;
        #region 打开数据库
        private static void ConnDB()
        {
            try
            {
                Action getConn = new Action(() =>
                {
                    conn = new SQLiteConnection(connStr);
                    conn.Open();
                });
                if (string.IsNullOrEmpty(DBName))
                    throw new Exception("未指定数据库！");
                connStr = $"Data Source={ DBName};Pooling=True;Page Size=1024;Cache Size=4096;FailMissing=True;Password={DBPass};";
                //connStr = $"Data Source={ DBName};Pooling=False;Max Pool Size=300;Page Size=1024;Cache Size=4096;Version=3;FailMissing=True;Password={DBPass};";                
                if (null == conn)
                    getConn();
                if (conn.State == ConnectionState.Broken | conn.State == ConnectionState.Closed)
                {
                    getConn();
                }
                if (conn.FileName != DBName)
                {
                    conn.Close();
                    getConn();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion
        #region 关闭数据库连接
        private static void CloseDB()
        {
            try
            {
                if (conn != null)
                {
                    conn.Close();
                    conn.Dispose();
                }
                conn = null;
            }
            catch (Exception)
            {
            }
        }
        #endregion
        #region 检查空参数
        private static void CheckParameter(ref SQLiteParameter[] op)
        {

        }
        #endregion
        #region 查询数据库
        public static DataTable Query(string sqlstr, SQLiteParameter[] op = null)
        {
            try
            {
                if (string.IsNullOrEmpty(sqlstr))
                    throw new Exception("命令不能为空！");
                ConnDB();
                SQLiteDataAdapter da = new SQLiteDataAdapter(sqlstr, conn);
                if (op != null)
                {
                    CheckParameter(ref op);
                    da.SelectCommand.Parameters.AddRange(op);
                    if (da.InsertCommand != null)
                        da.InsertCommand.Parameters.AddRange(op);
                    if (da.DeleteCommand != null)
                        da.DeleteCommand.Parameters.AddRange(op);
                    if (da.UpdateCommand != null)
                        da.UpdateCommand.Parameters.AddRange(op);
                }
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (null == dt)
                    throw new Exception("未知错误！");
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
        #region 执行命令
        public static void Exec(string sqlstr, SQLiteParameter[] op = null)
        {
            try
            {
                if (string.IsNullOrEmpty(sqlstr))
                    throw new Exception("命令不能为空！");
                ConnDB();
                SQLiteCommand cmd = new SQLiteCommand(sqlstr, conn);
                if (op != null)
                {
                    CheckParameter(ref op);
                    cmd.Parameters.AddRange(op);
                }
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
    }
}
